package com.yueya.api.comment;

import net.sf.json.JSONObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.yueya.business.comment.model.Comment;
import com.yueya.common.service.BaseService;
import com.yueya.common.util.DateUtil;
import com.yueya.common.util.UUIDGenerator;
import com.yueya.config.GlobalParam;

public class ICommentService extends BaseService {

	// 增加评论
	@Before(Tx.class)
	public Map<String, Object> addComment(JSONObject jo) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> error = new HashMap<String, Object>();

		if (containsKey(jo, "refid", "userid", "content")) {
			String refId = jo.getString("refid");
			String userId = jo.getString("userid");
			String content = jo.getString("content");

			Comment comment = new Comment();
			comment.set("comment_id", UUIDGenerator.getUUID()).set("ref_id", refId).set("user_id", userId)
					.set("content", content).set("create_time", DateUtil.getDate(DateUtil.timeFormat));
			boolean bl = comment.save();
			if (bl) {
				map.put("result", true);
			} else {
				map.put("result", false);
				error.put("errcode", "SUBMIT_UNSUCCESSFUL");
				error.put("errmsg", "提交失败");
				map.put("error", error);
			}

		} else {
			map.put("result", false);
			error.put("errcode", "INVALID_JSON_PARAM");
			error.put("errmsg", "JSON参数不合法");
			map.put("error", error);
		}

		return map;
	}

	/**
	 * 获取评论列表
	 * 
	 * @param jo
	 * @return
	 */
	public Map<String, Object> getPageList(JSONObject jo) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> error = new HashMap<String, Object>();

		if (containsKey(jo, "pgn", "pgs", "refid")) {

			pageNumber = jo.getInt("pgn");
			pageSize = jo.getInt("pgs");
			String refId = jo.getString("refid");

			String select = "select c.comment_id commentid,c.content,c.create_time createtime,c.user_id userid,u.user_name username,ub.gender"
					+ ",( CASE  ub.avatar_path WHEN null THEN  ub.avatar_path ELSE CONCAT('" + GlobalParam.getAttUrl()
					+ "' ,ub.avatar_path )  END  ) avatarpath";
			StringBuffer sql = new StringBuffer(
					" from t_comment c left join t_user u on u.user_id = c.user_id  left join t_user_base ub on u.user_id =ub.user_id where c.del_flag = '0' and c.ref_id = '"
							+ refId + "' ");
			sql.append(" order by c.create_time desc,c.update_time desc");

			Page<Record> page = Db.paginate(pageNumber, pageSize, select.toString(), sql.toString());
			if (page != null) {
				map.put("result", true);
				List<Record> list = page.getList();
				Record r = null;
				for (int i = 0; i < list.size(); i++) {
					r = list.get(i);
					// 发布时间格式化
					String createTime = r.getStr("createtime");
					if (StrKit.notBlank(createTime)) {
						r.set("createtime", DateUtil.betweenBeforeTime(createTime));
					}

				}
				map.put("data", list);
				map.put("size", page.getTotalRow());
				map.put("pgn", page.getPageNumber());
				map.put("pgs", page.getPageSize());
				map.put("totalPage", page.getTotalPage());
			} else {
				map.put("result", true);
				map.put("size", 0);
			}
		} else {
			map.put("result", false);
			error.put("errcode", "YY.INVALID_JSON_PARAM");
			error.put("errmsg", "JSON参数不合法");
			map.put("error", error);
		}
		return map;
	}

}